# GameView
#
# Author: Steve Streza
#
# $Id: GameView.py 329 2008-10-17 03:03:46Z stevestreza $

from Tkinter import *
from GameListView import *
from GameInputView import *
from NewGameFrame import *
from Queue import Queue

class GameView(object):
	"""sets up the main window and menu for the program"""
	menu = None
	gameController = None
	eventQueue = Queue()
	eventsRunning = False
	
	def __init__(self, rootView):
		"""
		Constructor for GameView
		
		Arguments:
		rootView - the Tk root widget
		"""
		self.root = rootView;
		self.tk = self.root #compatibility
		self.frame = Frame(self.root, width=300, height=588)
		
		self.listView = GameListView(self.frame, width=300, height=488)
		self.inputView = GameInputView(self.frame, width=300, height=100)
		
		self.listView.grid(row=0)
		self.inputView.grid(row=1)
		
	def executeOnMainLoop(self, funcPtr):
		"""
		Schedules a function to execute on the main thread. Since Tk doesn't like separate
		threads modifying widget state, pass functions into this method to have them
		execute by the main loop.
		
		Arguments:
		funcPtr - the function to run on the main thread
		"""
		self.eventQueue.put(funcPtr)
		if not self.eventsRunning:
			self.root.after(10, self.processQueue)

	def processQueue(self):
		"""
		Processes one queued event on the main thread. If there are items still in the queue, 
		it will reschedule itself in Tk's main loop.
		
		Do NOT call this directly unless you know you're running on Tk's main loop.
		"""
		self.eventsRunning = True
		if self.eventQueue.qsize() > 0:
			funcPtr = self.eventQueue.get(0)
			
			funcPtr()
			
			if self.eventQueue.qsize() == 0:
			    self.eventsRunning = False
		else:
		    self.eventsRunning = False

		if self.eventsRunning:
			self.root.after(10, self.processQueue)
		
	def getRoot(self):
		"""
		Returns the Tk root widget.
		"""
		return self.root
		
	def getRootInputView(self):
		"""
		Returns the container class of the input view.
		"""
		return self.inputView
	
	def setMenu(self,menu):
		"""
		Sets the menu bar for the root widget.
		"""
		self.root.config(menu=menu)

	def getListView(self):
		"""
		Returns the list view for the game stack.
		"""
		return self.listView
		
	def set_input_view(self,newInputView):
		"""
		Places the newInputView inside the input container. This is scheduled to run
		on the main loop automatically. Therefore, this method is thread-safe.
		"""
		def updater():
		    self.inputView.set_input_view(newInputView)
		self.executeOnMainLoop(updater)
		
	def set_game_controller(self,controller):
		"""
		Sets the GameController.
		"""
		self.gameController = controller
		
	def Game_Run_Loop(self):
		"""
		start Tk's main loop
		"""
		self.root.mainloop()

def InitializeGameView(gameController):
	"""initialize"""
	root = Tk()
	root.title("Mastermind")
	gameView = GameView(root)
	gameView.set_game_controller(gameController)
	gameView.frame.grid(row=0,ipadx=0,ipady=0,padx=0,pady=0)	
	return gameView

if __name__ == '__main__':
	gv = InitializeGameView(None)
	gv.Game_Run_Loop()
